package com.nf.health.app.activity.home;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.home.adapter.InformationNoticeAdapter;
import com.nf.health.app.activity.home.bean.InformationNotice;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.http.Act;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 首页中的信息公告
 */

public class InformationBoardActivity extends BaseActivity {
    public static  String HID_FLAG = "com.nf.hid";
    @Bind(R.id.lv_information_notice)
    DragListView lv_information_notice;
    private List<InformationNotice> list;
    private InformationNoticeAdapter informationNoticeAdapter;
    private View empetyView;
    private String hid = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContent(R.layout.activity_informationboard);
        setTitleBar(R.layout.titlebar_base);
        ButterKnife.bind(this);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "信息公告");
        initView();
        regisitListener();
        loadData();
    }


    private void regisitListener() {
        lv_information_notice.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {

            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {

            }
        });
    }

    private void initView() {
        hid  = getIntent().getStringExtra(HID_FLAG);
        list = new ArrayList<InformationNotice>();
        informationNoticeAdapter = new InformationNoticeAdapter(InformationBoardActivity.this,list);
        lv_information_notice.setRefreshableAndLoadMoreable(false, false);
        empetyView =View.inflate(this,R.layout.empty_nodata,null);
        TextView tv_empty = (TextView) empetyView.findViewById(R.id.tv_empty);
        tv_empty.setText("暂无数据");
        tv_empty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadData();
            }
        });
        lv_information_notice.setAdapter(informationNoticeAdapter);
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        switch (actionName){
            case Act.Flag.FLAG_INFORMATIONNOTICE_M:
                lv_information_notice.completeRefresh();
                lv_information_notice.completeLoadMore();
                  list = (List<InformationNotice>) object;
                if(list != null && list.size() >0){
                    if (informationNoticeAdapter == null) {
                        informationNoticeAdapter = new InformationNoticeAdapter(InformationBoardActivity.this, list);
                        lv_information_notice.setAdapter(informationNoticeAdapter);
                    } else {
                        informationNoticeAdapter.setMynotifyDataChanged(list);
                    }
                }else {
                    lv_information_notice.setEmptyView(empetyView);
                }

                break;

        }
    }

    private void loadData() {
        provider.getInformationNotice(hid);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

}
